// 2025/3/16
// https://leetcode.cn/problems/binary-tree-right-side-view/description/?envType=study-plan-v2&envId=top-100-liked
// 二叉树的右视图

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        vector<int> rightSideView(TreeNode* root) {
            if(root == nullptr) return (vector<int>){};
            vector<TreeNode*> v1, v2;
            vector<int> ans;
            v1.emplace_back(root);
            ans.emplace_back(root->val);
            while(!v1.empty())
            {
                for(auto& e : v1)
                {
                    if(e->left) v2.emplace_back(e->left);
                    if(e->right) v2.emplace_back(e->right);
                }
                if(!v2.empty())
                    ans.emplace_back((v2[v2.size() - 1])->val);
                swap(v1, v2);
                v2.clear();
            }
            return ans;
        }
    };